//
//  ZYAlreadyDownsController.m
//  优学荟管理系统
//
//  Created by edz on 2018/1/9.
//  Copyright © 2018年 星道三好. All rights reserved.
//
#import "ProgramAleadyDownVC.h"
#import "ZYAlreadyDownsController.h"
#import "ZYAllSegment.h"
#import "ZYAlreadyDownOneController.h"
#import "ZYAlreadyDownTwoController.h"
#import "ZYAlreadyDownThreeController.h"
//Segment高度
#define kSegmentH 40
@interface ZYAlreadyDownsController ()<UIScrollViewDelegate,ZYAllSegmentDelegate,UITextFieldDelegate>
@property (nonatomic, strong) UIScrollView *contentScrollView;
@property (nonatomic, strong) ZYAllSegment *segment;
@property (nonatomic,strong) UIView *allView;
@property (nonatomic,strong) UITextField *searchTextField;
@property (nonatomic,strong) ZYAlreadyDownOneController  *video;
@property (nonatomic,strong) ZYAlreadyDownTwoController *live;
@property (nonatomic,strong) ZYAlreadyDownThreeController *nearby;
@property (nonatomic,assign) NSInteger page;
@property (nonatomic,strong) UIButton *searchButton;

@end

@implementation ZYAlreadyDownsController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"已下架";
    //加载Segment
    [self setSegment];
    //加载ViewController
    [self addChildViewController];
    //加载ScrollView
    [self setContentScrollView];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTargat:self action:@selector(leftBarButtonItemAction) image:@"icon_back_student_sign" highImage:@"icon_back_student_sign"];
}





- (void)leftBarButtonItemAction{
    
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
    
    
}









-(void)setSegment {
    
    //初始化
    ZYAllSegment *segment = [[ZYAllSegment alloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width, kSegmentH)];
    segment.delegate = self;
    self.segment = segment;
    self.segment.titleList = [[NSMutableArray alloc]initWithObjects:@"金牌课程",@"加强课",@"三好小当家",@"少年编程",nil];
    [self.view addSubview:segment];
    
    
}
//加载ScrollView
-(void)setContentScrollView {
    
    UIScrollView *sv = [[UIScrollView alloc]initWithFrame:CGRectMake(0,kSegmentH, self.view.frame.size.width, self.view.frame.size.height - kSegmentH)];
    [self.view addSubview:sv];
    sv.bounces = NO;
    sv.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    sv.contentOffset = CGPointMake(0, 0);
    sv.pagingEnabled = YES;
    sv.showsHorizontalScrollIndicator = NO;
    sv.scrollEnabled = YES;
    sv.userInteractionEnabled = YES;
    sv.delegate = self;
    
    for (int i=0; i<self.childViewControllers.count; i++) {
        UIViewController * vc = self.childViewControllers[i];
        vc.view.frame = CGRectMake(i * kScreenWidth, 0, kScreenWidth, self.view.frame.size.height - kSegmentH -60);
        [sv addSubview:vc.view];
        
    }
    
    sv.contentSize = CGSizeMake(self.segment.titleList.count * kScreenWidth, 0);
    self.contentScrollView = sv;
}
//加载3个ViewController
-(void)addChildViewController{
    
    
    self.video = [ZYAlreadyDownOneController new];
    self.live = [ZYAlreadyDownTwoController new];
    self.nearby = [ZYAlreadyDownThreeController new];
    ProgramAleadyDownVC *vc = [ProgramAleadyDownVC new];
    [self addChildViewController:self.video];
    [self addChildViewController:self.live];
    [self addChildViewController:self.nearby];
    
    [self addChildViewController:vc];
    
    
}

#pragma mark - UIScrollViewDelegate
//实现LGSegment代理方法
-(void)scrollToPage:(int)Page {
    
    
    NSLog(@"%d",Page);
    
    self.page = Page;
    
    CGPoint offset = self.contentScrollView.contentOffset;
    offset.x = self.view.frame.size.width * Page;
    [UIView animateWithDuration:0.3 animations:^{
        self.contentScrollView.contentOffset = offset;
        
        
        
    }];
}
// 只要滚动UIScrollView就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [self.searchTextField resignFirstResponder];
    
    CGFloat offsetX = scrollView.contentOffset.x;
    
    
    
    [self.segment moveToOffsetX:offsetX];
    
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    CGFloat offsetX = scrollView.contentOffset.x;
    int page = offsetX/kScreenWidth;
    
    
    self.page = page;
    
}












- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
    [self.searchTextField resignFirstResponder];
    
    
}







@end
